package org.seven.jrdp.biz.t.service;

import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.seven.jrdp.biz.t.mapper.TCrkMapper;
import org.seven.jrdp.biz.t.mapper.TKqMapper;
import org.seven.jrdp.biz.t.mapper.TKqMxMapper;
import org.seven.jrdp.biz.t.mapper.TRkdMapper;
import org.seven.jrdp.biz.t.mapper.TRkdMxMapper;
import org.seven.jrdp.biz.t.model.TCrk;
import org.seven.jrdp.biz.t.model.TKq;
import org.seven.jrdp.biz.t.model.TKqMx;
import org.seven.jrdp.biz.t.model.TRkd;
import org.seven.jrdp.biz.t.model.TRkdMx;
import org.seven.jrdp.commons.config.ParamConfig;
import org.seven.jrdp.commons.exception.BizException;
import org.seven.jrdp.commons.model.Option;
import org.seven.jrdp.commons.mybatis.model.PageRequest;
import org.seven.jrdp.commons.util.DateTimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
public class TRkdService {
	@Autowired
	private TKqMapper tKqMapper;
	@Autowired
	private TKqMxMapper tKqMxMapper;
	@Autowired
	private TRkdMapper tRkdMapper;
	@Autowired
	private TRkdMxMapper tRkdMxMapper;
	@Autowired
	private TKqMxService tKqMxService;
	@Autowired
	private TCrkMapper tCrkMapper;

	@Transactional(readOnly = true)
	public PageInfo<Map<String, Object>> find(Map<String, Object> map, PageRequest pageRequest) {
		PageHelper.startPage(pageRequest.getPage(), pageRequest.getRows());
		List<Map<String, Object>> list = tRkdMapper.find(map);
		for (Map<String, Object> rkd : list) {
			List<String> kqmc = tRkdMapper.findKqmc((String) rkd.get("cpbh"));
			Set<String> set = new LinkedHashSet<String>();
			for (String s : kqmc) {
				set.add(s);
			}
			rkd.put("kqmc", set);
		}
		return new PageInfo<Map<String, Object>>(list);
	}

	@Transactional(readOnly = true)
	public PageInfo<Map<String, Object>> findQxrk(Map<String, Object> map, PageRequest pageRequest) {
		PageHelper.startPage(pageRequest.getPage(), pageRequest.getRows());
		List<Map<String, Object>> list = tRkdMapper.findQxrk(map);
		for (Map<String, Object> rkd : list) {
			List<String> kqmc = tRkdMapper.findKqmc((String) rkd.get("cpbh"));
			Set<String> set = new LinkedHashSet<String>();
			for (String s : kqmc) {
				set.add(s);
			}
			rkd.put("kqmc", set);
		}
		return new PageInfo<Map<String, Object>>(list);
	}

	@Transactional(readOnly = true)
	public TRkd get(String id) {
		return tRkdMapper.selectByPrimaryKey(id);
	}

	@Transactional
	public void insert(TRkd tRkd) {
		if (tRkdMapper.getCountWwc() > 0) {
			throw new BizException("有未完成的入库单，不允许添加新的入库单");
		}
		if (get(tRkd.getCpbh()) != null) {
			throw new BizException("产品编号不允许重复");
		}
		tRkd.setRkzt("00");
		tRkdMapper.insertSelective(tRkd);
	}

	@Transactional
	public void delete(String id) {
		TRkd tRkd = get(id);
		if ("00".equals(tRkd.getRkzt())) {
			tRkdMapper.deleteByPrimaryKey(id);
		} else {
			throw new BizException("入库单中包含明细，不允许删除");
		}
	}

	@Transactional
	public void upload(List<String[]> list) {
		int index = 1;
		for (String[] s : list) {
			String cpbh = s[0];
			String cpxh = s[1];
			String ggxh = s[2];
			String rkrq = s[3];
			String zl = s[4];
			String ks = s[5];
			String kqmc = s[6];
			TRkd tRkd = new TRkd();
			tRkd.setCpbh(cpbh);
			tRkd.setGgxh(ggxh);
			tRkd.setRkrq(DateTimeUtils.getDate(rkrq));
			tRkd.setRkzt("20");
			tRkd.setCreateBy("admin");
			tRkd.setCreateDate(new Date());
			if (tRkdMapper.selectByPrimaryKey(cpbh) == null) {
				tRkdMapper.insertSelective(tRkd);
			}
			TRkdMx tRkdMx = new TRkdMx();
			tRkdMx.setCpbh(cpbh);
			tRkdMx.setCpxh(cpxh);
			tRkdMx.setZl(Integer.valueOf(zl));
			tRkdMx.setKs(Integer.valueOf(ks));
			tRkdMx.setKqmc(kqmc);
			tRkdMx.setRkry("admin");
			tRkdMx.setRksj(new Date());
			tRkdMx.setEwm(getEwm(tRkd, tRkdMx));
			try {
				tRkdMxMapper.insertSelective(tRkdMx);
				Thread.sleep(10);
			} catch (Exception e) {
				throw new BizException("第" + index + "行，插入入库明细出错");
			}
			TKqMx tKqMx = tRkdMxToTKqMx(tRkdMx);
			tKqMxMapper.insertSelective(tKqMx);
			if (tKqMapper.selectByPrimaryKey(kqmc) == null) {
				throw new BizException("第" + index + "行，没有对应的库区名称");
			}
			TKq tKq = new TKq();
			tKq.setKqmc(kqmc);
			tKq.setKqzt("20");
			tKqMapper.updateByPrimaryKeySelective(tKq);
			index++;
		}
	}

	@Transactional
	public String insertMx(TRkdMx tRkdMx1, TRkdMx tRkdMx2) {
		TRkd tRkd = get(tRkdMx1.getCpbh());
		if (tRkd == null) {
			throw new BizException("未创建入库单，不允许插入明细");
		}
		if ("20".equals(tRkd.getRkzt())) {
			throw new BizException("入库单已完成，不允许插入明细");
		}
		String kqmc = tKqMxService.getKqmc(tRkd.getGgxh());
		updateRkzt(tRkd.getCpbh(), "10", tRkdMx1.getRkry());
		tRkdMx1.setKqmc(kqmc);
		tRkdMx1.setRksj(DateTimeUtils.getDate());
		tRkdMx1.setEwm(getEwm(tRkd, tRkdMx1));
		tRkdMxMapper.insertSelective(tRkdMx1);
		TKqMx tKqMx1 = tRkdMxToTKqMx(tRkdMx1);
		tKqMxMapper.insertSelective(tKqMx1);
		tRkdMx2.setKqmc(kqmc);
		tRkdMx2.setRksj(DateTimeUtils.getDate());
		tRkdMx2.setEwm(getEwm(tRkd, tRkdMx2));
		tRkdMxMapper.insertSelective(tRkdMx2);
		TKqMx tKqMx2 = tRkdMxToTKqMx(tRkdMx2);
		tKqMxMapper.insertSelective(tKqMx2);
		int rkzl = tRkdMapper.getRkzl(kqmc);
		int zlwc = ParamConfig.getZlwc();
		int min = rkzl - zlwc;
		int max = rkzl + zlwc;
		int yrkzl = tRkdMapper.getYrkzl(tRkd.getGgxh());
		if (yrkzl >= min && yrkzl <= max) {
			TKq tKq = new TKq();
			tKq.setKqmc(kqmc);
			tKq.setKqzt("20");
			tKqMapper.updateByPrimaryKeySelective(tKq);
			tKqMxMapper.updateMxzt(tRkd.getGgxh());
		} else {
			int kqks = tRkdMapper.getKqks(kqmc);
			int yrkks = tRkdMapper.getYrkks(kqmc);
			if (kqks - yrkks == 0) {
				TKq tKq = new TKq();
				tKq.setKqmc(kqmc);
				tKq.setKqzt("20");
				tKqMapper.updateByPrimaryKeySelective(tKq);
			} else {
				TKq tKq = new TKq();
				tKq.setKqmc(kqmc);
				tKq.setKqzt("10");
				tKqMapper.updateByPrimaryKeySelective(tKq);
			}
		}
		return kqmc;
	}

	@Transactional
	public String insertMx(TRkdMx tRkdMx1) {
		TRkd tRkd = get(tRkdMx1.getCpbh());
		if (tRkd == null) {
			throw new BizException("未创建入库单，不允许插入明细");
		}
		if ("20".equals(tRkd.getRkzt())) {
			throw new BizException("入库单已完成，不允许插入明细");
		}
		String kqmc = tKqMxService.getKqmc(tRkd.getGgxh());
		updateRkzt(tRkd.getCpbh(), "10", tRkdMx1.getRkry());
		tRkdMx1.setKqmc(kqmc);
		tRkdMx1.setRksj(DateTimeUtils.getDate());
		tRkdMx1.setEwm(getEwm(tRkd, tRkdMx1));
		tRkdMxMapper.insertSelective(tRkdMx1);
		TKqMx tKqMx1 = tRkdMxToTKqMx(tRkdMx1);
		tKqMxMapper.insertSelective(tKqMx1);
		int rkzl = tRkdMapper.getRkzl(kqmc);
		int zlwc = ParamConfig.getZlwc();
		int min = rkzl - zlwc;
		int max = rkzl + zlwc;
		int yrkzl = tRkdMapper.getYrkzl(tRkd.getGgxh());
		if ("AD10.1".equals(tRkd.getGgxh())) {
			if (Integer.valueOf(tRkdMapper.getYrkks(kqmc)) % 2 == 0) {
				if (yrkzl >= min && yrkzl <= max) {
					TKq tKq = new TKq();
					tKq.setKqmc(kqmc);
					tKq.setKqzt("20");
					tKqMapper.updateByPrimaryKeySelective(tKq);
					tKqMxMapper.updateMxzt(tRkd.getGgxh());
				} else {
					int kqks = tRkdMapper.getKqks(kqmc);
					int yrkks = tRkdMapper.getYrkks(kqmc);
					if (kqks - yrkks == 0) {
						TKq tKq = new TKq();
						tKq.setKqmc(kqmc);
						tKq.setKqzt("20");
						tKqMapper.updateByPrimaryKeySelective(tKq);
					} else {
						TKq tKq = new TKq();
						tKq.setKqmc(kqmc);
						tKq.setKqzt("10");
						tKqMapper.updateByPrimaryKeySelective(tKq);
					}
				}
			} else {
				TKq tKq = new TKq();
				tKq.setKqmc(kqmc);
				tKq.setKqzt("10");
				tKqMapper.updateByPrimaryKeySelective(tKq);
			}
		} else {
			if (yrkzl >= min && yrkzl <= max) {
				TKq tKq = new TKq();
				tKq.setKqmc(kqmc);
				tKq.setKqzt("20");
				tKqMapper.updateByPrimaryKeySelective(tKq);
				tKqMxMapper.updateMxzt(tRkd.getGgxh());
			} else {
				int kqks = tRkdMapper.getKqks(kqmc);
				int yrkks = tRkdMapper.getYrkks(kqmc);
				if (kqks - yrkks == 0) {
					TKq tKq = new TKq();
					tKq.setKqmc(kqmc);
					tKq.setKqzt("20");
					tKqMapper.updateByPrimaryKeySelective(tKq);
				} else {
					TKq tKq = new TKq();
					tKq.setKqmc(kqmc);
					tKq.setKqzt("10");
					tKqMapper.updateByPrimaryKeySelective(tKq);
				}
			}
		}
		return kqmc;
	}

	@Transactional(readOnly = true)
	public int getCountMx(String cpbh) {
		return tRkdMapper.getCountMx(cpbh);
	}

	@Transactional
	public void wcrk(String cpbh) {
		TRkd tRkd = get(cpbh);
		if (tRkd == null) {
			throw new BizException("入库单已删除");
		}
		if ("00".equals(tRkd.getRkzt())) {
			throw new BizException("入库单计划中，不允许完成入库");
		}
		if ("20".equals(tRkd.getRkzt())) {
			throw new BizException("入库单已完成，不需要重复操作");
		}
		if ("30".equals(tRkd.getRkzt())) {
			throw new BizException("入库单已取消入库，不允许完成入库");
		}
		updateRkzt(cpbh, "20", null);
	}

	@Transactional
	public void qxwc(String cpbh) {
		TRkd tRkd = get(cpbh);
		if ("00".equals(tRkd.getRkzt())) {
			throw new BizException("入库单计划中，不允许取消完成");
		}
		if ("10".equals(tRkd.getRkzt())) {
			throw new BizException("入库单未完成，不允许取消完成");
		}
		if ("30".equals(tRkd.getRkzt())) {
			throw new BizException("入库单已取消入库，不允许取消完成");
		}
		if (!cpbh.equals(tRkdMapper.getMaxCpbh())) {
			throw new BizException("只允许取消最新的入库单");
		}
		Example example = new Example(TCrk.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("cpbh", cpbh);
		if (tCrkMapper.selectCountByExample(example) > 0) {
			throw new BizException("已创建出库单，不允许取消完成");
		}
		updateRkzt(cpbh, "10", null);
	}

	@Transactional
	public void xgrkd(String cpbh1, String cpbh, Date rkrq, String xgry) {
		Example example = new Example(TCrk.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("cpbh", cpbh);
		if (tCrkMapper.selectCountByExample(example) > 0) {
			throw new BizException("已创建出库单，不允许修改入库单");
		}
		TRkd tRkd = tRkdMapper.selectByPrimaryKey(cpbh);
		tRkd.setCpbh(cpbh1);
		tRkd.setRkrq(rkrq);
		Example example1 = new Example(TRkdMx.class);
		Criteria criteria1 = example1.createCriteria();
		criteria1.andEqualTo("cpbh", cpbh);
		List<TRkdMx> list = tRkdMxMapper.selectByExample(example1);
		for (TRkdMx tRkdMx : list) {
			tRkdMx.setCpbh(cpbh1);
			String ewm = getEwm(tRkd, tRkdMx);
			tRkdMapper.xgrkd1(cpbh1, cpbh, tRkdMx.getCpxh(), ewm);
			tRkdMapper.xgrkd2(cpbh1, cpbh, tRkdMx.getCpxh(), ewm);
		}
		tRkdMapper.xgrkd(cpbh1, cpbh, rkrq, xgry, new Date());
	}

	@Transactional
	public void xgrkmx(String cpbh, String cpxh, int ks) {
		TRkd tRkd = tRkdMapper.selectByPrimaryKey(cpbh);
		TRkdMx tRkdMx = new TRkdMx();
		tRkdMx.setCpbh(cpbh);
		tRkdMx.setCpxh(cpxh);
		tRkdMx = tRkdMxMapper.selectByPrimaryKey(tRkdMx);
		tRkdMx.setKs(ks);
		String ewm = getEwm(tRkd, tRkdMx);
		tRkdMapper.xgrkmx(cpbh, cpxh, ks, ewm);
		tRkdMapper.xgrkmx1(cpbh, cpxh, ks, ewm);
	}

	@Transactional(readOnly = true)
	public List<Option> findCpxh(String cpbh) {
		return tRkdMapper.findCpxh(cpbh);
	}

	@Transactional(readOnly = true)
	public List<Option> findKcCpxh(String cpbh) {
		return tRkdMapper.findKcCpxh(cpbh);
	}

	@Transactional
	public void qxrk(String cpbh) {
		updateRkzt(cpbh, "30", null);
		tKqMxMapper.deleteByCpbh(cpbh);
		tKqMxMapper.updateKqzt();
	}

	@Transactional(readOnly = true)
	public List<Map<String, Object>> findRkd() {
		return tRkdMapper.findRkd();
	}

	@Transactional(readOnly = true)
	public List<Map<String, Object>> findRkdMx(String cpbh) {
		return tRkdMapper.findRkdMx(cpbh);
	}

	@Transactional(readOnly = true)
	public Map<String, Object> getRkdMx(String cpbh, String cpxh) {
		return tRkdMapper.getRkdMx(cpbh, cpxh);
	}

	@Transactional
	public void deleteRkdMx(String cpbh, String cpxh, String user) {
		int max = getCountMx(cpbh);
		if (max != Integer.valueOf(cpxh)) {
			throw new BizException("请先删除序号最大的产品");
		}
		TKqMx tKqMx = new TKqMx();
		tKqMx.setCpbh(cpbh);
		tKqMx.setCpxh(cpxh);
		tKqMx = tKqMxMapper.selectByPrimaryKey(tKqMx);
		if ("1".equals(tKqMx.getMxzt())) {
			throw new BizException("已到达入库重量，无法删除");
		}
		TRkdMx tRkdMx = new TRkdMx();
		tRkdMx.setCpbh(cpbh);
		tRkdMx.setCpxh(cpxh);
		TKq tKq = new TKq();
		tKq.setKqmc(tRkdMxMapper.selectByPrimaryKey(tRkdMx).getKqmc());
		tKq.setKqzt("10");
		tKqMapper.updateByPrimaryKeySelective(tKq);
		tRkdMxMapper.deleteByPrimaryKey(tRkdMx);
		if (getCountMx(cpbh) == 0) {
			updateRkzt(cpbh, "00", user);
		}
		tKqMxMapper.deleteByPrimaryKey(tKqMx);
		tKqMxMapper.updateKqzt();
	}

	@Transactional(readOnly = true)
	public Map<String, Object> getDy(String cpbh, PageRequest pageRequest) {
		Map<String, Object> map = tRkdMapper.getDy(cpbh);
		PageHelper.startPage(pageRequest.getPage(), pageRequest.getRows());
		List<Map<String, Object>> list = tRkdMapper.findDy(cpbh);
		for (Map<String, Object> dy : list) {
			List<Map<String, Object>> dymx = tRkdMapper.findDyMx((String) dy.get("cpbh"), (String) dy.get("kqmc"));
			dy.put("dymx", dymx);
		}
		map.put("list", list);
		return map;
	}

	@Transactional
	private void updateRkzt(String cpbh, String rkzt, String rkry) {
		TRkd tRkd = new TRkd();
		tRkd.setCpbh(cpbh);
		tRkd.setRkzt(rkzt);
		tRkd.setUpdateBy(rkry);
		tRkdMapper.updateByPrimaryKeySelective(tRkd);
	}

	private String getEwm(TRkd tRkd, TRkdMx tRkdMx) {
		String ewm = "R031      A00000000001             ";
		ewm += tRkdMx.getCpbh() + "-" + tRkdMx.getCpxh() + "             ";
		ewm += tRkdMx.getCpxh() + "   ";
		ewm += DateFormatUtils.format(tRkd.getRkrq(), "yyyyMMdd") + "       ";
		ewm += tRkdMx.getZl() + "  ";
		ewm += tRkdMx.getKs() + "                                                               ";
		return ewm;
	}

	private TKqMx tRkdMxToTKqMx(TRkdMx tRkdMx) {
		TKqMx tKqMx = new TKqMx();
		tKqMx.setCpbh(tRkdMx.getCpbh());
		tKqMx.setCpxh(tRkdMx.getCpxh());
		tKqMx.setKqmc(tRkdMx.getKqmc());
		tKqMx.setZl(tRkdMx.getZl());
		tKqMx.setKs(tRkdMx.getKs());
		tKqMx.setRksj(tRkdMx.getRksj());
		tKqMx.setEwm(tRkdMx.getEwm());
		tKqMx.setMxzt("0");
		return tKqMx;
	}
}